/**
 * @file    If_num.c
 * @brief   驱动接口-存储接口
 * @author  Liuwei
 * @version 1.0.1
 * @date    2023-04-27
 * 
 * @copyright Copyright (c) 2023 JBD-Energy Storage Technology Co. LTD
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version   <th>Author   <th>Description
 * <tr><td>2023-04-27 <td>1.0.1     <td>Liuwei    <td>首次创建
 * </table>
 */

/* includes ------------------------------------------------------------------*/
#include "If_nvm.h"
#include "dev_eep_24cxx.h"
#include "drv_i2c_soft.h"
#include "FreeRTOS.h"
#include "semphr.h"
/* macro/define --------------------------------------------------------------*/

/* typedef -------------------------------------------------------------------*/

/* local functions -----------------------------------------------------------*/

/* static variables ----------------------------------------------------------*/
static SemaphoreHandle_t xSemaphore = NULL;
/* global variables ----------------------------------------------------------*/

/** ------------------------------------------------------------------------- *
  *                             Global function
 ** ------------------------------------------------------------------------- */
/**
 * @brief  存储初始化
 * @return uint8_t 
 */
uint8_t If_nvm_init(void)
{
    xSemaphore = xSemaphoreCreateMutex();

    drv_i2c_soft_init(EEP_DRV_I2C);

    return dev_eep_check_online();
}

/**
 * @brief  获取存储器容量
 * @return uint32_t 
 */
uint32_t If_nvm_get_cap(void)
{
    uint32_t eep_cap = 0;
    xSemaphoreTake(xSemaphore, 100 );
	
    eep_cap = dev_eep_get_capacity();
	
    xSemaphoreGive(xSemaphore);
    return eep_cap;
}

/**
 * @brief  存储数据
 * @param  addr : 存储地址
 * @param  data_buf : 数据缓冲区
 * @param  len : 长度
 */
uint8_t If_nvm_save_data(uint32_t addr , uint8_t *data_buf , uint16_t len)
{
	uint8_t ret = 0;
	
    xSemaphoreTake(xSemaphore, 100 );
	
    ret = dev_eep_write_bytes(addr , data_buf , len);
      
	xSemaphoreGive(xSemaphore);
	
	return ret;
}

/**
 * @brief  读取数据
 * @param  addr : 存储地址
 * @param  data_buf : 数据缓冲区
 * @param  len : 长度
 */
uint8_t If_nvm_read_data(uint32_t addr , uint8_t *data_buf , uint16_t len)
{
	uint8_t ret = 0;
	
    xSemaphoreTake(xSemaphore, 100 );
	
    ret = dev_eep_read_bytes(addr , data_buf , len);
	
	xSemaphoreGive(xSemaphore);
	
	return ret;
}
/** ------------------------------------------------------------------------- *
  *                              Local function
 ** ------------------------------------------------------------------------- */
 
/********** Copyright (c) 2023 JBD-Energy Storage Technology Co. LTD *********/


